package com.xgk.boot.module.core.dal.mapper.channel;

import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.mybatis.core.dataobject.BaseDO;
import com.xgk.boot.framework.mybatis.core.mapper.BaseMapperX;
import com.xgk.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xgk.boot.module.core.controller.admin.channel.vo.sp.SpPageReqVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageReqVO;
import com.xgk.boot.module.core.dal.entity.channel.ApiServiceDO;
import com.xgk.boot.module.core.dal.entity.channel.ServiceProviderDO;
import com.xgk.boot.module.core.dal.entity.channel.ServiceProviderDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.Instant;
import java.time.LocalDate;

@Mapper
public interface ServiceProviderMapper extends BaseMapperX<ServiceProviderDO> {

    default ServiceProviderDO selectByName(String name) {
        return selectOne(ServiceProviderDO::getSpName, name);
    }

    default ServiceProviderDO selectByCode(String code) {
        return selectOne(ServiceProviderDO::getSpCode, code);
    }
    default PageResult<ServiceProviderDO> selectPage(SpPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ServiceProviderDO>()
                .likeIfPresent(ServiceProviderDO::getSpName, reqVO.getSpName())
                .likeIfPresent(ServiceProviderDO::getSpName, reqVO.getSpCode())
                .eqIfPresent(ServiceProviderDO::getSpStatus, reqVO.getStatus())
                .betweenIfPresent(BaseDO::getCreateTime, new Instant[]{reqVO.getStartTime(), reqVO.getEndTime()})
                .orderByAsc(ServiceProviderDO::getUpdateTime));
    }


    @Select("SELECT a.* FROM service_provider a " +
            "JOIN service_provider_api_rela r ON a.sp_id = r.sp_id " +
            "WHERE r.as_id = #{apiId} AND a.sp_status = 0  and a.deleted =0")
    ServiceProviderDO selectProviderByApiId(@Param("apiId") Long apiId);
}
